﻿
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class HallPanel : MonoBehaviour
{
    /// <summary>
    /// 设置功能打开按钮
    /// </summary>
    public Button settingBtn;
    /// <summary>
    /// 设置界面
    /// </summary>
    public GameObject settingPanel;
    /// <summary>
    /// 大关卡预制件
    /// </summary>
    private LevelContent LevelContent;
    /// <summary>
    /// 小关卡预制件
    /// </summary>
    private Map level;

    public Transform contentTrans;


    public List<LevelData> levelDatas = new List<LevelData>();
    /// <summary>
    /// 设置声音按钮
    /// </summary>
    public  Button voiceBtn;

    public GameObject yinsibtn;
    public GameObject morebtn;
    public GameObject quitbtn;
    public GameObject addiconbtn;
    public GameObject privacypanel;

    private void Awake()
    {
        level = Resources.Load<Map>("Prefab/level");
        LevelContent = Resources.Load<LevelContent>("Prefab/levelcontent");

        
    }
    void Start()
    {
        if (AdManager.FirstLoad)
        {
            AdManager.Instance.AddBtn(yinsibtn, morebtn, quitbtn, addiconbtn);
            AdManager.FirstLoad = false;
        }



        settingPanel.SetActive(false);
        settingBtn.onClick.AddListener(OnClickSettingBtn);
        voiceBtn.onClick.AddListener(()=>AudioManager.Instance.isOpenSound());
        InitBigLevel();
    }

    private void OnClickSettingBtn()
    {
        settingPanel.SetActive(true);
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private List<Map> map_list=new List<Map>();

    void InitBigLevel()
    {
        for (int i = contentTrans.childCount - 1; i >= 0; i--)
        {
            Destroy(contentTrans.GetChild(i).gameObject);
        }
        Dictionary<int, MapData> DataDic = ReadData.instance.mapDataDic;

        if (DataDic.Count>0)
        {
            foreach(int key in DataDic.Keys)
            {
                Map map = Instantiate(level, contentTrans);
                map.SetInfo(key);
                map_list.Add(map);
                Texture2D texture = Resources.Load<Texture2D>("Texture2D/" + key);
                Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));// 将Texture2D转换为Sprite
                map.GetComponent<Transform>().Find("bg/Image").GetComponent<Image>().sprite = sprite;

                if (!PlayerPrefs.HasKey(key.ToString()))
                {
                    Material grayscaleMaterial = new Material(Shader.Find("UI/Grayscale"));
                    map.GetComponent<Transform>().Find("bg/Image").GetComponent<Image>().material = grayscaleMaterial;
                }
            }
        }
    }


    public void ApplyGrayscale(Texture2D texture)
    {
        // 创建一个材质使用灰度Shader
        Material grayscaleMaterial = new Material(Shader.Find("Custom/Grayscale"));

        // 创建一个临时RenderTexture
        RenderTexture rt = RenderTexture.GetTemporary(texture.width, texture.height);

        // 应用灰度Shader
        Graphics.Blit(texture, rt, grayscaleMaterial);

        // 将结果读回Texture2D
        RenderTexture.active = rt;
        texture.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
        texture.Apply();

        // 清理
        RenderTexture.active = null;
        RenderTexture.ReleaseTemporary(rt);
    }

    private void OnEnable()
    {
         GameInit.instance.ReturnAllSlots();
        //Refresh_Display();
        InitBigLevel();

        if (!AdManager.Instance.ShieldAddress) AdManager.Instance.ShowNative();
    }

    /// <summary>
    /// 刷新关卡界面
    /// </summary>
    public void Refresh_Display()
    {
        for(int i=0;i < map_list.Count;i++)
        {
            if (!PlayerPrefs.HasKey(map_list[i].id.ToString()))
            {
                map_list[i].GetComponent<Transform>().Find("bg/Image").GetComponent<Image>().color = new Color(0.5f, 0.5f, 0.5f);
            }
        }
    }


    /// <summary>
    /// 隐私
    /// </summary>
    public void Yinsi()
    {
        AdManager.Instance.Hide(privacypanel);
    }
    public void More()
    {
        AdManager.Instance.MoreGame();
    }
    public void Quit()
    {
        AdManager.Instance.Quit();
    }
    /// <summary>
    /// 添加桌面
    /// </summary>
    public void Addicon()
    {
        AdManager.Instance.AddDesktopIcon();
    }


}



